package com.levi.juc.coll_unsafe.map;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * hashmap是不安全的，高并发情况下会出现ConcurrentModificationException
 * 解决方案  要用concurrentHashmap   比hashTable性能高,hashtable中的方法都加了同步锁
 *
 *
 * hashmap默认初始容量是16，加载因子是0.75f
 * 如果传入的初始容量大于了最大容量就会设置为最大容量
 */
public class HashMapTest {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        for (int i = 1; i <= 20; i++) {
            new Thread(()->{
                map.put(Thread.currentThread().getName(), "value");
                System.out.println(map);
            }).start();
        }

        Map<String, String> map1 = new ConcurrentHashMap<>();
        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                map1.put(Thread.currentThread().getName(), "value");
                System.out.println(map1);
            }).start();
        }
        Hashtable<Object, Object> hashtable = new Hashtable<>();
    }
}
